<template>
  <div class="level-list">
    <el-tabs v-model="activeName">
      <el-tab-pane label="基础信息" name="first">
        <levelForm :formInfo="formInfo" :levelType="levelType" :disabled="true" :joinOrgHidden="true" @saveData="saveData"></levelForm>
      </el-tab-pane>
      <el-tab-pane label="质控点信息" name="second">
        <!-- 搜索 -->
        <listSearch @search="search">
          <template slot="handle">
            <el-button type="primary" plain @click="handle('add', formInfo)">添加质控点</el-button>
          </template>
        </listSearch>
        <!-- 表格 -->
        <myTable :column="column" :tableData="tableData" :total="total" @changePage="changePage">
          <el-table-column label="操作" width="186">
            <template slot-scope="scope">
              <el-button type="text" @click="handle('detail', scope.row)">详情</el-button>
              <el-button type="text" @click="handle('target', scope.row)">设置目标值及预警规则</el-button>
            </template>
          </el-table-column>
        </myTable>
      </el-tab-pane>
    </el-tabs>
    <div class="back-btn">
      <!-- 提交审核以后不可再提交，examineState(0-未审核 1-审核中 2-审核通过 3-审核不通过)-->
      <el-button v-permission="{type:levelType,per:[146,148,150,152,154]}" type="success" v-if="formInfo && !formInfo.examineState" @click="submitCheck">提交</el-button>
      <back-button></back-button>
    </div>

    <!-- 添加质控点|设置目标值 -->
    <addPoint v-if="addParams.visible" :addParams="addParams" :orgId="userInfo.orgId" @addOrEdit="addOrEdit" @getList="getList"></addPoint>
  </div>
</template>

<script>
import listSearch from '@/components/search/listSearch'
import levelForm from '@/views/targetSystem/targetManage/module/levelFrom'
import addPoint from '@/views/targetSystem/targetManage/module/addPoint'
import myTable from '@/components/table/index.vue'
import { updateMyTargetData, getMyTargetById, getMyPointList, delMyPointData, editMyPointData, submitToCheck, addMyPointData } from '@/api/target'
import { filterParams, getStorage } from '@/config/utils'

export default {
  name: 'myTargetList',
  components: {
    listSearch,
    addPoint,
    levelForm,
    myTable
  },
  data() {
    return {
      levelType: '',
      activeName: 'first',
      column: [
        {
          prop: 'indexOne',
          label: '一级指标'
        },
        {
          prop: 'indexTwo',
          label: '二级指标'
        },
        {
          prop: 'indexThree',
          label: '三级指标'
        },
        {
          prop: 'name',
          label: '质控点'
        },
        {
          prop: 'intension',
          label: '质控点内涵'
        },
        {
          prop: 'diagnosisStandard',
          label: '诊断标准'
        },
        {
          prop: 'belongPartName',
          label: '责任部门'
        },
        {
          prop: 'valueType',
          label: '值类型'
        },
        {
          prop: 'standardValue',
          label: '标准值'
        },
        {
          prop: 'targetValue',
          label: '目标值'
        },
        {
          prop: 'expectedValue',
          label: '预期值'
        },
        {
          prop: 'isWarning',
          label: '是否预警',
          asyncData: this.$config.yesOrNo
        },
        {
          prop: 'isOptional',
          label: '是否可选',
          asyncData: this.$config.isOptional
        }
      ],
      tableData: [],
      total: 0,
      searchFrom: {
        targetStandardId: '',
        indexOne: '',
        indexTwo: '',
        indexThree: '',
        name: '',
        pageSize: 10,
        pageIndex: 1
      },
      addParams: {
        visible: false,
        title: '添加质控点',
        type: ''
      },
      formInfo: null,
      dialogParams: {
        visible: false,
        title: '设置目标值',
        type: 'target',
        data: {}
      },
      userInfo: getStorage('userInfo')
    }
  },
  created() {
    this.searchFrom.targetStandardId = this.$route.query.id
    this.levelType = this.$route.params.type
    this.getList()
    this.getMyTargetById()
  },
  methods: {
    // 添加质控点
    handle(type, data) {
      switch (type) {
        case 'add':
          this.addParams = {
            visible: true,
            title: '添加质控点',
            type: type,
            pointType: 'myTarget',
            joinOrgDTOList: [{ relationId: this.userInfo.orgId }],
            targetStandardId: data.id,
            showNote: true
          }
          break
        case 'detail':
          this.addParams = {
            visible: true,
            title: '查看质控点',
            pointType: 'show',
            type: type,
            id: data.id,
            targetStandardId: data.targetStandardId,
            data: data,
            showNote: true
          }
          break
        case 'edit':
          this.addParams = {
            visible: true,
            title: '编辑质控点',
            type: type,
            id: data,
            showNote: true
          }
          break
        case 'del':
          this.$confirm('您确定要删除这些数据吗?', '批量删除', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              delMyPointData(data)
                .then(res => {
                  this.$message.success('操作成功')
                  this.getList()
                })
                .catch()
            })
            .catch()
          break
        case 'target':
          // sourceId不为空表示下发的质控点，为空或不存在就是自定义的质控点
          this.addParams = {
            visible: true,
            title: '设置目标值',
            type: type,
            pointType: type, // 用于区分要不要显示目标值和标准值
            data: data,
            id: data.id,
            targetStandardId: data.targetStandardId,
            isCustomPoint: !(data.sourceId && data.sourceId > 0),
            isWaitDeal: this.$route.query.isWaitDeal,
            waitDealId: this.$route.query.waitDealId,
            showNote: true
          }
          break
      }
    },
    // 修改基础信息
    saveData(data) {
      updateMyTargetData(this.searchFrom.targetStandardId, data)
        .then(res => {
          this.$message.success('操作成功')
        })
        .catch()
    },
    // 查询质控点列表
    getList() {
      const params = filterParams(this.searchFrom)
      getMyPointList(params)
        .then(res => {
          this.tableData = res.dataList || []
          this.total = res.total || 0
        })
        .catch(() => {})
    },
    // 搜索
    search(searchFrom) {
      this.searchFrom.pageIndex = 1
      this.searchFrom.indexOne = searchFrom.indexOne
      this.searchFrom.indexTwo = searchFrom.indexTwo
      this.searchFrom.indexThree = searchFrom.indexThree
      this.searchFrom.name = searchFrom.name
      this.searchFrom.belongPart = searchFrom.belongPart
      this.getList()
    },
    // 查询我的目标标准详情
    getMyTargetById() {
      if (this.searchFrom.targetStandardId) {
        getMyTargetById(this.searchFrom.targetStandardId)
          .then(res => {
            this.formInfo = res || {}
          })
          .catch(() => {})
      }
    },
    // 提交至审核
    submitCheck() {
      this.$confirm('您确定要提交至审核，提交后不可修改吗?', '提交至审核', {
        confirmButtonText: '提交',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          submitToCheck(this.searchFrom.targetStandardId)
            .then(res => {
              this.$message.success('操作成功')
              this.getList()
            })
            .catch(() => {})
        })
        .catch()
    },
    // 编辑或新增质控点
    addOrEdit(type, params) {
      switch (type) {
        case 'edit':
          editMyPointData(this.addParams.id, params)
            .then(res => {
              this.$message.success('操作成功')
              this.addParams.visible = false
              this.getList()
            })
            .catch(() => {})
          break
        case 'add':
          addMyPointData(params)
            .then(res => {
              this.$message.success('操作成功')
              this.addParams.visible = false
              this.getList()
            })
            .catch(() => {})
          break
      }
    },
    // 切换分页
    changePage(searchForm) {
      this.searchFrom.pageIndex = searchForm.pageIndex
      this.searchFrom.pageSize = searchForm.pageSize
      this.getList()
    }
  },
  watch: {
    $route: {
      handler(val) {
        // 如果index为2,默认打开second的tab页，默认显示质控点信息
        if (val.query.index === 2) {
          this.activeName = 'second'
        }
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style scoped>
</style>
